package com.ruoyi.web.controller.app;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.ResponseResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.GoogleAuthenticationTool;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.biz.CashBizService;
import com.ruoyi.system.domain.Cash;
import com.ruoyi.system.domain.CashHistory;
import com.ruoyi.system.dto.TransferSubmitDto;
import com.ruoyi.system.service.CashHistoryService;
import com.ruoyi.system.service.CashService;
import com.ruoyi.system.service.OrderService;
import com.ruoyi.system.vo.CashCountVo;
import com.ruoyi.system.vo.CashVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Api(tags = "用户端:钱包相关接口")
@RestController
@RequestMapping("/app/cash")
public class CashController extends AppController {
    @Resource
    private CashService cashService;
    @Resource
    private CashBizService cashBizService;
    @Resource
    private CashHistoryService cashHistoryService;
    @Resource
    private OrderService orderService;


    @Log(title = "转账", businessType = BusinessType.INSERT)
    @ApiOperation(value = "转账")
    @PostMapping("transfer")
    public ResponseResult transfer(@Valid @RequestBody TransferSubmitDto dto) {
        SysUser user = getSysUser();
        GoogleAuthenticationTool.checkGoogleCode(user.getRandomSecretKey(), dto.getCode());
        cashBizService.transfer(user, dto);
        return this.setSuccessResult();
    }


    @ApiOperation(value = "钱包金额")
    @PostMapping("info")
    public ResponseResult<CashVo> info() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = this.getUserId();
        Cash cash = cashService.getByUserId(userId);
        CashHistory cashHistory = new CashHistory();
        cashHistory.setUserId(loginUser.getUserId());
        cashHistory.setSource("user");
        cashHistory.setChangeType("add");
        cashHistory.setType("earning");
        DateTime thisDay = DateUtil.beginOfDay(new Date());

        DateTime dateTime = DateUtil.offsetDay(thisDay, -1);
//今日收益 包含粉丝收益
        BigDecimal todayEarning = cashHistoryService.sum("changeMoney", cashHistory, thisDay, new Date());
        BigDecimal yestodayEarning = cashHistoryService.sum("changeMoney", cashHistory, dateTime, thisDay);
        CashVo cashVo = BeanUtil.copyProperties(cash, CashVo.class);
        cashVo.setTodayEarning(todayEarning);
        cashVo.setYesterdayEarning(yestodayEarning);
        //计算冻结金额
        BigDecimal sum = orderService.getFreeze(cash.getUserId());
        cashVo.setFreezeEarning(sum);
        return this.setSuccessResult(cashVo);
    }

    @ApiOperation(value = "余额明细")
    @RequestMapping("list")
    public ResponseResult<List<CashHistory>> list(CashHistory cashHistory) {
        if (StrUtil.isBlank(cashHistory.getOrderNo())) {
            cashHistory.setOrderNo(null);
        }
        this.startPage();
        // CashHistory cashHistory = new CashHistory();
        cashHistory.setUserId(this.getUserId());
        List<CashHistory> list = cashHistoryService.findList(cashHistory);
        return this.setSuccessResult(list);
    }

    @ApiOperation(value = "钱包总额")
    @GetMapping("getCash")
    public ResponseResult getCash() {
//        this.startPage();
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        CashHistory cashHistory = new CashHistory();
//        cashHistory.setUserId(loginUser.getUserId());
//        List<CashHistory> list = cashHistoryService.findList(cashHistory);

        CashCountVo cashCountVo = orderService.countByUserId(this.getUserId());


        return this.setSuccessResult(cashCountVo);
    }

    @ApiOperation(value = "统计下级贡献金额")
    @RequestMapping("getRecommend")
    public ResponseResult getRecommend() {
        Long userId = this.getUserId();
        CashHistory cashHistory = new CashHistory();
        cashHistory.setUserId(userId);
        cashHistory.setRemark("下级贡献收益");
        DateTime thisDay = DateUtil.beginOfDay(new Date());
        BigDecimal recommend = cashHistoryService.sum("changeMoney", cashHistory, thisDay, new Date());
        return this.setSuccessResult(recommend);
    }


}
